<template >
	<view>
		<template v-if="Object.keys(goods).length"><!-- template一般用于条件判断显示，block一般用于循环显示 -->
			<view class="u-text-center">
				<image mode="aspectFit" :src="goods.cover_url"></image>
			</view>
			<view class="u-p-20 u-p-b-80">
				<!-- 商品基本信息 -->
				<view class="base-info">
					<text class="title u-font-40">{{goods.title}}</text>
					<view class="u-flex u-row-between u-m-t-20 u-m-b-20">
						<text class="price u-font-32">￥{{goods.price}}</text>
						<text class="small">库存:{{goods.stock}}</text>
						<text class="small">销量:{{goods.sales}}</text>
					</view>
				</view>
				<!-- tabs -->
				<view style="display: flex;justify-content: center;" class=" u-m-b-20">
					<u-tabs 
						:list="tabsList" 
						:activeStyle="{
							color: '#2979ff',
							fontWeight: 'bold',
							transform: 'scale(1.05)'}" 
						:is-scroll="false" 
						@change="tabsChang">
					</u-tabs>
				</view>
				<!-- 商品详情区 -->
				<template v-if="current==0">
					<u-parse :content="goods.details"></u-parse>
				</template>
				<!-- 商品评论区 -->
				<template v-if="current==1">
					<template v-if="goods.comments.length">
						<view class="comment" v-for="(res, index) in goods.comments" :key="res.id">
							<view class="left"><image :src="res.user.avatar_url" mode="aspectFill"></image></view>
							<view class="right">
								<view class="top">
									<view class="name">{{ res.user.name }}</view>
									<!-- <view class="like" :class="{ highlight: res.isLike }"> -->
									<view class="like" >
										<view class="num">{{ res.star }}</view>
										<!-- <u-icon v-if="!res.isLike" name="thumb-up" :size="30" color="#9a9a9a" @click="getLike(index)"></u-icon> -->
										<u-icon name="thumb-up" :size="30" color="#9a9a9a" ></u-icon>
										<!-- <u-icon v-if="res.isLike" name="thumb-up-fill" :size="30" @click="getLike(index)"></u-icon> -->
									</view>
								</view>
								<view class="content">{{ res.content }}</view>
								<view class="reply-box">
									<view class="item" >
										<view class="text">{{ res.reply }}</view>
									</view>
								</view>
								<view class="bottom">
									{{ res.created_at }}
								</view>
							</view>
						</view>
					</template>
					<template v-else>
						<!-- 评论为空 -->
						<u-empty
							text="暂无评论"
							icon="http://cdn.uviewui.com/uview/empty/comment.png">
						</u-empty>
					</template>
				</template>
				<!-- 商品推荐区 -->
				<template v-if="current==2">
					<template v-if="like_goods.length">
						<u-row class="u-flex-wrap">
							<u-col span="6" v-for="(item,index) in like_goods" :key="index">
								<goodsCard :item="item" ></goodsCard>
							</u-col>
						</u-row>
					</template>
					<template v-else>
						<!-- 推荐商品为空 -->
						<u-empty
							mode="list"
							text="暂无推荐商品">
						</u-empty>
					</template>
				</template>
			</view>
			<!-- 提交订单栏 -->
			<view class="navigation">
				<view class="left">
					<view class="item" @click="toCollect">
						<u-icon v-if="!collect" name="heart" :size="40" color="#606266"></u-icon>
						<u-icon v-else name="heart-fill" :size="40" color="#2979ff"></u-icon>
						<view :style="{color: collect==1?'#2979ff':'#606266'}" class="text u-line-1">{{collect?"已收藏":"收藏"}}</view>
					</view>
					<view class="item" @click="toCart">
						<u-badge numberType="overflow" max="99" :absolute="true" shape="horn" :offset="[-5,-18]" type="error" :value="cartNum" ></u-badge>
						<u-icon name="shopping-cart" :size="40" color="606266"></u-icon>
						<view class="text u-line-1">购物车</view>
					</view>
				</view>
				<view class="right">
					<u-button :disabled="goods.stock?false:true" class="cart btn" @click="addCart" type="primary">加入购物车</u-button>
				</view>
			</view>
		</template>
		<template v-else>
			<view class="loading">
				<u-loading-icon text="拼命加载中"  :vertical = 'true' textSize="18" size="30"></u-loading-icon>
			</view>
		</template>
	</view>
</template>

<script>
	let access_token;
	export default {
		data() {
			return {
				goodsID: '',
				goods: {},
				like_goods:{},
				tabsList:[
					{name:'商品详情'},
					{
						name:'商品评论',
						badge: {
							value: 0,
						}},
					{name:'推荐商品'}],
				current:0,
				collect:false,
				cartNum:0
			}
		},
		onLoad(options) {
			access_token = uni.getStorageSync('access_token');
			this.goodsID = options.id;
			this.getData();
			this.cartLen();
		},
		methods: {
			tabsChang(e){
				this.current = e.index;
			},
			async getData(){
				let res = await uni.$u.http.get(`/api/goods/${this.goodsID}`,{header: {
						Authorization: 'bearer ' + access_token
					}});
				console.log(res);
				this.goods = res.goods;
				this.like_goods = res.like_goods;
				this.collect = res.goods.is_collect;
				this.tabsList[1].badge.value = res.goods.comments.length;
			},
			// 收藏节流
			toCollect(){
				this.$u.throttle(this.collectNot,1500)
			},
			// 收藏和取消收藏
			async collectNot(){
				if (!uni.$u.utils.isLogin()) { //先判断是否登录
					return;
				}
				// 请求收藏接口
				await uni.$u.http.post(`/api/collects/goods/${this.goodsID}`,{},{header:{Authorization:'bearer '+access_token}});
				// 收藏和取消收藏提示
				if(this.collect){
					uni.$u.toast("取消收藏成功")
				}else{
					uni.$u.toast("收藏成功")
				}
				this.collect=!this.collect;
			},
			// 添加购物车(自带节流效果)
			async addCart(){
				if(!uni.$u.utils.isLogin()){}// 如果没有登录先去登录
				else{
					// 发送请求
					const res= await uni.$u.http.post('/api/carts',{goods_id:this.goodsID},{header:{Authorization:'bearer '+access_token}});
					uni.$u.toast("添加购物车成功！");
					this.cartNum++;
				}
			},
			async cartLen(){
				//先判断是否登录，如果未登录，则不显示购物车数量。
				if (!access_token) { 
					return;
				}
				console.log(access_token);
				const res = await uni.$u.http.get(`/api/carts`,{params:{include:'goods'},header:{Authorization:'bearer '+access_token}});
				this.cartNum=res.data.reduce((pre,item)=>{
					 return parseInt(pre) + parseInt(item.num);
				},0)
			},
			toCart(){
				uni.$u.route({
					type:'switchTab',
					url:'/pages/cart/cart'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.loading {
		text-align: center;
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);/* 元素会以自身的左上角为原点，往上往左分别移动自身长宽的50%，以使其居于中心位置。 */
	}
	.title,
	.price {
		font-weight: bolder;
	}
	
	.price {
		color: rgb(209, 45, 58);
	}
	.small{
		font-size: 24rpx;
		color: #ccc;
	}
	.comment {
		display: flex;
		padding: 30rpx;
		.left {
			image {
				width: 64rpx;
				height: 64rpx;
				border-radius: 50%;
				background-color: #f2f2f2;
			}
		}
		.right {
			flex: 1;
			padding-left: 20rpx;
			font-size: 30rpx;
			.top {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 10rpx;
				.name {
					color: #5677fc;
				}
				.like {
					display: flex;
					align-items: center;
					color: #9a9a9a;
					font-size: 26rpx;
					.num {
						margin-right: 4rpx;
						color: #9a9a9a;
					}
				}
				.highlight {
					color: #5677fc;
					.num {
						color: #5677fc;
					}
				}
			}
			.content {
				margin-bottom: 10rpx;
			}
			.reply-box {
				background-color: rgb(242, 242, 242);
				border-radius: 12rpx;
				.item {
					padding: 20rpx;
					border-bottom: solid 2rpx $u-border-color;
					.username {
						font-size: 24rpx;
						color: #999999;
					}
				}
				.all-reply {
					padding: 20rpx;
					display: flex;
					color: #5677fc;
					align-items: center;
					.more {
						margin-left: 6rpx;
					}
				}
			}
			.bottom {
				margin-top: 20rpx;
				display: flex;
				font-size: 24rpx;
				color: #9a9a9a;
				.reply {
					color: #5677fc;
					margin-left: 10rpx;
				}
			}
		}
	}
	.navigation {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		display: flex;
		border: solid 2rpx #f2f2f2;
		background-color: #ffffff;
		padding: 16rpx 0;
		justify-content: space-between;
		.left {
			flex:3;
			display: flex;
			font-size: 20rpx;
			justify-content: space-around;
			.item {
				position: relative;
				text-align: center;
			}
		}
	
		.right {
			flex:9;
			display: flex;
			font-size: 28rpx;
			justify-content: flex-end;
			align-items: center;
			/* #ifdef MP-WEIXIN */
			padding: 0 15px;
			/* #endif */
			.btn {
				text-align: center;
				line-height: 66rpx;
				width: 90%;
				border-radius: 10rpx;
				color: #ffffff;
			}
	
			.cart {
				background-color: #2979ff;
				margin-right: 30rpx;
			}
	
			.buy {
				background-color: #ff7900;
			}
		}
	}
</style>
